package com.cj.springtest2.dao.impl;

import com.cj.springtest2.dao.IUserDao;
import com.cj.springtest2.model.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.Reader;
import java.util.List;

/**
 * Created by chenjun on 2016/10/13.
 */
@Repository(value="userDao")
public class UserDaoImpl extends BasicDaoImpl implements IUserDao {

    private IUserDao userDao;

//    public UserDaoImpl() {
//
//    }

    @Override
    protected void registerDao() {
        userDao = session.getMapper(IUserDao.class);
    }

    public void save(User user) {
//        String statement = "userMapper.save";
//        session.insert(statement, user);
        userDao.save(user);
        session.commit();  //一定要记得commit
    }

    public boolean update(User user) {
//        String statement = "userMapper.update";
//        session.update(statement, user);
        userDao.update(user);
        session.commit();  //一定要记得commit
        return true;
    }

    public boolean delete(int id) {
//        String statement = "userMapper.delete";
//        session.delete(statement, id);
        userDao.delete(id);
        session.commit();  //一定要记得commit
        return true;
    }

    public User findById(int id) {
//        String statement = "userMapper.findById";
//        User user = (User)session.selectOne(statement, id);
        return userDao.findById(id);
    }

    public User findByUserName(String userName) {
//        String statement = "userMapper.findByUserName";
//        User user = (User) session.selectList(statement, userName);
        System.out.println("userDao: " + userDao);
        return userDao.findByUserName(userName);
    }

    public List<User> findAll() {
//        String statement = "userMapper.findAll";
//        List<User> userList = (List)session.selectList(statement);
        return userDao.findAll();
    }
}
